package com.xli.organ.ou.frameou.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xli.dto.component.TreeNode;
import com.xli.dto.component.model.TableModel;
import com.xli.dto.component.model.TreeModel;
import com.xli.dto.controller.BaseController;
import com.xli.dto.params.IdsDTO;
import com.xli.dto.params.TreeNodeDTO;
import com.xli.dto.result.ResultVO;
import com.xli.dto.result.status.Status;
import com.xli.dto.validation.group.IGroup;
import com.xli.organ.ou.frameou.entity.FrameOu;
import com.xli.organ.ou.frameou.entity.dto.OuAddDTO;
import com.xli.organ.ou.frameou.entity.dto.OuSearchDTO;
import com.xli.organ.ou.frameou.entity.dto.OuUpdateDTO;
import com.xli.organ.ou.frameou.entity.mapper.IFrameOuMapper;
import com.xli.organ.ou.frameou.entity.vo.OuVO;
import com.xli.organ.ou.frameou.service.IFrameOuService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@Tag(name = "部门管理")
@PreAuthorize("hasAuthority('frame:ou:view')")
@RestController
@RequestMapping("/frameOu")
public class FrameOuController extends BaseController {

    @Autowired
    private IFrameOuService iFrameOuService;

    @Operation(summary = "保存部门")
    @PreAuthorize("hasAuthority('frame:ou:add')")
    @PostMapping(value = "/add")
    public ResultVO<String> add(@RequestBody @Validated(IGroup.add.class) OuAddDTO dto) {
        FrameOu frameOu = IFrameOuMapper.INSTANCE.toEntity(dto);
        if (iFrameOuService.insert(frameOu)) {
            return new ResultVO<>(Status.SUCCESS, "保存成功");
        }
        return new ResultVO<>(Status.FAILED, "保存失败");
    }

    @Operation(summary = "删除部门")
    @PreAuthorize("hasAuthority('frame:ou:delete')")
    @PostMapping(value = "/delete")
    public ResultVO<String> delete(@RequestBody @Validated(IGroup.delete.class) IdsDTO dto) {
        int i = 0;
        for (String id : dto.getIds()) {
            if (iFrameOuService.delete(Long.valueOf(id))) {
                i++;
            }
        }
        return new ResultVO<>(Status.SUCCESS, i + "条数据删除成功");
    }

    @Operation(summary = "更新部门")
    @PreAuthorize("hasAuthority('frame:ou:update')")
    @PostMapping(value = "/update")
    public ResultVO<String> update(@RequestBody @Validated(IGroup.update.class) OuUpdateDTO dto) {
        FrameOu frameOu = IFrameOuMapper.INSTANCE.toEntity(dto);
        if (iFrameOuService.update(frameOu)) {
            return new ResultVO<>(Status.SUCCESS, "保存成功");
        }
        return new ResultVO<>(Status.FAILED, "保存失败");
    }

    @Operation(summary = "查询详情")
    @PreAuthorize("hasAuthority('frame:ou:list')")
    @PostMapping(value = "/detail/{id}")
    public ResultVO<OuVO> detail(@PathVariable("id") String id) {
        FrameOu frameOu = iFrameOuService.find(Long.valueOf(id));
        if (frameOu != null) {
            return new ResultVO<>(Status.SUCCESS, "查询成功", IFrameOuMapper.INSTANCE.toVO(frameOu));
        }
        return new ResultVO<>(Status.SUCCESS, "查询失败");
    }

    @Operation(summary = "查询列表")
    @PreAuthorize("hasAuthority('frame:ou:list')")
    @PostMapping(value = "/search")
    public ResultVO<TableModel<OuVO, FrameOu, OuSearchDTO>> search(@RequestBody @Validated(IGroup.search.class) OuSearchDTO dto) {

        TableModel<OuVO, FrameOu, OuSearchDTO> tableModel = new TableModel<>(dto) {
            @Override
            public List<OuVO> fetch(int current, int pageSize) {
                Page<FrameOu> page = iFrameOuService.findList(getQueryWrapper(), current, pageSize);
                this.setTotal(page.getTotal());
                return page.convert(IFrameOuMapper.INSTANCE::toVO).getRecords();
            }
        };
        return new ResultVO<>(Status.SUCCESS, "查询成功", tableModel);
    }

    @Operation(summary = "查询部门树")
    @PreAuthorize("hasAuthority('frame:ou:list')")
    @PostMapping(value = "/fetchTree")
    public ResultVO<TreeModel<OuVO>> fetchTree(@RequestBody @Validated(IGroup.search.class) Optional<TreeNodeDTO> dto) {

        TreeModel<OuVO> treeModel = new TreeModel<>() {
            @Override
            public List<TreeNode<OuVO>> fetch() {
                List<TreeNode<OuVO>> treeNodeList = new ArrayList<>();
                List<FrameOu> frameOuList = iFrameOuService.findList(new QueryWrapper<>());
                for (FrameOu frameOu : frameOuList) {
                    OuVO ouVO = IFrameOuMapper.INSTANCE.toVO(frameOu);
                    TreeNode<OuVO> childNode = new TreeNode<>();
                    childNode.setId(frameOu.getId());
                    childNode.setText(frameOu.getOu_name());
                    childNode.setPid(frameOu.getPid());
                    treeNodeList.add(childNode);
                }

                return treeNodeList;
            }
        };
        return new ResultVO<>(Status.SUCCESS, "查询成功", treeModel);
    }
}
